package com.exam.demo.service.impl;

import com.exam.demo.mapper.StudentMapper;
import com.exam.demo.pojo.Student;
import com.exam.demo.pojo.StudentExample;
import com.exam.demo.pojo.Teacher;
import com.exam.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    @Transactional
    //注册方法,成功返回1，否则返回0
    public int add(Student student) {
        return this.studentMapper.insert(student);
    }

    @Override
    @Transactional
    //登录方法，返回登录是否成功
    public Boolean login(String stuNo, String stuPwd) {
        Student student = this.studentMapper.selectByPrimaryKey(stuNo);
        if(student.getStuPwd().equals(stuPwd))
            return true;
        else return false;
    }

    @Override
    public Student getInfo(String stuNo) {
        Student student = this.studentMapper.selectByPrimaryKey(stuNo);
        student.setStuPwd(null);
        return student;
    }

    @Override
    @Transactional
    //修改个人资料,只更新提交的字段,成功返回1，否则返回0
    public int updateStuInfo(Student student) {
        return this.studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    @Transactional
    //修改密码,虽然和上面那个方法一样，但是分的清楚一点,成功返回1，否则返回0,原密码错误返回2
    public int updateStuPwd(String stuNo,String oldStuPwd,String newStuPwd){
        Student student = this.studentMapper.selectByPrimaryKey(stuNo);
        if(oldStuPwd.equals(student.getStuPwd())){
            student.setStuPwd(newStuPwd);
            return this.studentMapper.updateByPrimaryKeySelective(student);
        }
        else return 2;
    }

    @Override
    public List<Student> getStudentListByStuNo(String stuNo) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andStuNoLike(stuNo+"%");
        return this.studentMapper.selectByExample(studentExample);
    }

    @Override
    public List<Student> getStudentListByStuName(String stuName) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andStuNameEqualTo(stuName);
        return this.studentMapper.selectByExample(studentExample);
    }

    @Override
    public List<Student> getStudentListByStuDiscipline(String stuDiscipline) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andStuDisciplineEqualTo(stuDiscipline);
        return this.studentMapper.selectByExample(studentExample);
    }

    @Override
    public List<Student> getStudentListByStuGrade(String stuGrade) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andStuGradeEqualTo(stuGrade);
        return this.studentMapper.selectByExample(studentExample);
    }


}
